<template>
  <!-- 4 lx属性管理 -->
  <div class="breadcrumb">
    <Breadcrumb :aaa="aaa" :msgg="msg" :txt="txt" :path1="path1" />
  </div>

  <div class="btn" style="margin: 20px">
    <div>数据列表</div>
    <el-button @click="onAddUser" type="primary">添加</el-button>
  </div>
  <div class="dataList" style="margin: 20px">
    <!-- 数据的展示 -->
    <el-table :data="tableData" style="width: 100%" :border="true">
      <el-table-column
        type="selection"
        fixed
        prop="id"
        disabled
        label=""
        width="60"
      >
      </el-table-column>
      <el-table-column prop="id" label="编号" width="100" />
      <el-table-column prop="name" label="属性名称" width="150" />
      <el-table-column prop="attributeCount" label="商品类型" width="180" />
      <el-table-column prop="paramCount" label="属性是否可选" width="180" />
      <el-table-column prop="paramCount" label="属性值的录入方式" width="180" />
      <el-table-column prop="paramCount" label="可选值列表" width="180" />
      <el-table-column prop="paramCount" label="排序" width="180" />
      <el-table-column label="操作" width="300" flex="right">
        <template #default="scope">
          <el-button @click="onEdit(scope.row)">编辑</el-button>
          <el-button type="danger">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="select-btn">
      <div class="select">
        <el-select v-model="select" placeholder="批量操作" style="width: 200px">
          <el-option label="删除" value="1" />
        </el-select>
        <el-button type="primary" style="margin-left: 20px">确定</el-button>
      </div>
      <!-- 分页器 -->
      <el-pagination
        v-model:current-page="pagination.pageNum"
        v-model:page-size="pagination.pageSize"
        background
        :page-sizes="[1, 5, 10, 20]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pagination.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import type { Ref } from "vue";
import * as api from "../../request/api";
import useTable from "../../hooks/useTables";
import type { IProductAtrrListItem } from "../../request/PmsProductAttributeCategoryController/index";
import { ElMessage, ElMessageBox } from "element-plus";
import Breadcrumb from "../../components/breadcrumb.vue";
let msg = ref("属性管理");
let txt = ref("商品");
let path1 = ref("pms/addProduct");
let aaa = ref(0);

// 新增
let add = false; // false编辑 true 新增
// 模态框
let modalForm = ref({}) as Ref<IProductAtrrListItem>;

let select = ref("");

let {
  tableData,
  getTableData,
  pagination,
  handleSizeChange,
  handleCurrentChange,
} = useTable(api.productAttr.getProductCategoryList);

// 弹框
let visible = ref(false);

// 添加用户
let onAddUser = () => {
  let newUser = {
    name: "",
  } as IProductAtrrListItem;
  modalForm.value = newUser;
  add = true;
  visible.value = true;
};
// 编辑
let onEdit = (row: IProductAtrrListItem) => {
  // console.log(row);
  add = false;
  modalForm.value = row;
  // console.log(modalForm);
  visible.value = true;
};

// 子级emit组件传过来的事件
let onCloseDialog = () => {
  visible.value = false;
};

// 确认了输入框的内容之后
// 需要发送请求
let onConfirmDialog = (row: IProductAtrrListItem) => {
  // console.log(row);  // row就是表单数据
  // 二次 确认
  ElMessageBox.confirm("是否确认提交!", "提示", {
    confirmButtonText: "OK",
    cancelButtonText: "Cancel",
    type: "warning",
  })
    .then(async () => {
      // 发请求
      // 去判断 是 编辑 还是添加
      if (add) {
        await api.productAttr.addProductAtrr(row); // 新增
      } else {
        await api.productAttr.updataProductAtrr(row); // 编辑
      }
      visible.value = false;
      ElMessage({
        type: "success",
        message: "操作成功",
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消提交",
      });
    });
};

getTableData();
</script>

<style scoped lang="scss">
.role {
  padding: 20px 15px 20px 15px;
  background-color: #fff;
  margin-bottom: 20px;
  .role_top {
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;
  }
}
.btn {
  padding: 20px 15px 20px 15px;
  background-color: #fff;
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
}
:deep .el-input {
  width: 200px !important;
}
:deep .el-button {
  font-size: 12px !important;
}
.breadcrumb {
  height: 45px;
  border-bottom: 1px solid #ebeef5;
  font-size: 20px;
}
.select-btn {
  margin-top: 20px;
  display: flex;
  justify-content: space-between;
}
</style>
